<?php
namespace app\index\model;
use think\Db;
class User extends \think\Model{
    /**
     * 判断是否登录
     */
    public function isLogin(){
        $rs = Db::name('user')->where('id',session('user_id'))->find();
        return $rs ? true : false;
        //return session('?user_id');
    }
    
    /**
     * 微信登陆
     */
    public function wxLogin( $uuid , $muid=0 ){
        $rs = Db::name('user')->where('uuid', $uuid)->find();
        if( $rs ){
            $user_id    = $rs['id'];
            $login_times= $rs['login_times'];
        }else{
            $d = [
                'uuid'          =>  $uuid,
                'create_time'   =>  time(),
            ];
            if($muid){
                $d['introduct_id'] = $muid;
                $d['parent_id']    = $muid;
            }
            $user_id     = Db::name('user')->insertGetId( $d );  
            $login_times = 0; 
        }  
        session('user_id',$user_id);
        $d = [
            "id"              => $user_id,
            "last_login_time" => time(),
            "last_login_ip"   => com_client_ip(),
            "login_times"     => $login_times+1,
            "error_times"     => 0,
        ];
        Db::name('user')->update($d);
    }

    /**
     * 退出登录
     * 清楚cookie和session
     */
    public function logOut(){
        \think\Session::clear();
        \think\Cookie::delete('user_id');
    }
}